<?php
/**
 * 打卡机器人
 */
namespace App\Bots;
use Cache;
use App\Models\User;
use App\Models\Movie;
class Watcher {


    public function getUser($num = 1)
    {
        $users = Cache::remember('all_inter_user', 60 * 60 * 24, function () {
            return User::where('type', 'douban')->pluck('id');
        });
        return $users->random($num);
    }
    public function getMovies($num = 1)
    {
        $movies = Cache::remember('all_movies', 60 * 60 * 24, function () {
            return Movie::where('score', '>=', 7)->where('score_people', '>=', 0)->pluck('id');
        });
        return $movies->random($num);
    }
    public function watching()
    {
        $user = $this->getUser()->first();
        $movie = Movie::find($this->getMovies()->first());
        $status = $movie->isWatcherBy($user);
        if ($status && $status['status'] == 'watching') {
            $movie->watched($user);
        } else {
            $movie->watching($user);
        }
    }

    public function watched()
    {
        $user_id = $this->getUser()->first();
        $user = User::find($user_id);
        if($movie = $user->watchings->first()) {
            $movie->watched($user);
        }
    }
}
